{*
SPDX-FileCopyrightText: © 2021 Olivier Meunier <olivier@neokraft.net>

SPDX-License-Identifier: AGPL-3.0-only
*}
{{ extends "./base" }}
{{- import "/_libs/forms" -}}


{{- block title() -}}
    {{ .Item.Name }} - Collections
{{- end -}}

{{- block mainContent() -}}
<h1 class="title text-h2">Collection {{ .Item.Name }}</h1>

{{- if .Item.IsDeleted -}}
  <form action="{{ urlFor(`./delete`) }}" method="post">
    {{ yield csrfField() }}
    <input type="hidden" name="_to" value="{{ currentPath }}" />
    {{- yield message(type="info") content -}}
      Collection will be removed in a few seconds&nbsp;
      <button class="btn-primary" name="cancel" value="1">Cancel</button>
    {{- end -}}
  </form>
{{- else -}}
  <turbo-frame id="bookmark-list"
  data-controller="turbo-refresh"
  data-turbo-refresh-interval-value="10"
  data-turbo-refresh-on-value="[data-bookmark-deleted='true']">
    <form action="{{ urlFor() }}?edit=1" method="post"
     class="my-4 p-2 bg-gray-100 border rounded">
      {{ yield formErrors(form=.Form) }}
      {{ yield csrfField() }}
      <details {{- if .Editing }} open{{- end -}}>
        <summary class="block link font-semibold"
        >{{- yield icon(name="o-pencil") }} Edit</summary>
        <div class="text-sm mt-2">
          {{ yield textField(
            field=.Form.Get("name"),
            label="Name",
            class="mb-2",
          ) }}
          {{ include("./components/filters") .Form }}
          <div class="mt-4 flex">
            <p class="flex-grow">
              <button class="btn btn-primary">Save</button>
            </p>
            <p>
              <button class="btn-danger" formaction="{{ urlFor(`./delete`) }}" formmethod="post"
              >{{ yield icon(name="o-trash") }} Delete collection</button>
            </p>
          </div>
        </div>
      </details>
    </form>

    {{- if hasPermission("api:bookmarks", "export") -}}
      <p class="mt-4 mb-6">
        <span class="font-semibold">{{ yield icon(name="o-download") }}
        Export this collection</span>:
        <a href="{{ urlFor(`/api/bookmarks/export.epub`) }}?collection={{ .Item.ID }}"
         class="link">Epub</a>
      </p>
    {{- end }}

    {{- include "./components/bookmark_list" -}}
  </turbo-frame>
{{- end -}}
{{- end -}}
